package xian.woniuxy.d_cache;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.Subject;

/**
 * @author gao
 * @time 2021/12/24 09:28:22
 */
public class App {
    public static void main(String[] args) {
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        // ====================================================================
        Realm realm = new MyRealm();
        // ====================================================================
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);

        EhCacheManager cacheManager = new EhCacheManager();
        // 设置缓存的配置文件
        cacheManager.setCacheManagerConfigFile("classpath:xian/woniuxy/d_cache/ehcache.xml");
        securityManager.setCacheManager(cacheManager);

        Subject subject = SecurityUtils.getSubject();
        String username = "foo";
        String password = "123";
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            System.out.println("认证通过");
        } catch (Exception e) {
            System.out.println("认证失败");
            e.printStackTrace();
        }

        System.out.println("认证是否通过：" + subject.isAuthenticated());
        System.out.println("身份是：" + subject.getPrincipal());

        System.out.println("user:save " + subject.isPermitted("user:save"));
        System.out.println("user:delete " + subject.isPermitted("user:delete"));
        System.out.println("user:update " + subject.isPermitted("user:update"));
        System.out.println("user:find " + subject.isPermitted("user:find"));
    }
}
